package cn.zhuzicc.reflect;

import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.List;
import java.util.Map;

/**
 * @author zx
 * @version 1.0
 * @className: Test11
 * @description: 反射获取泛型
 * @date: 2021/4/28 13:31
 */
public class Test11 {

    public static void main(String[] args) throws Exception {

        // 1. 获取泛型信息
        Class<Test11> c1 = Test11.class;
        Method method1 = c1.getMethod("test01", Map.class, List.class);

        // 1.1 获取方法参数泛型
        Type[] genericParameterTypes = method1.getGenericParameterTypes();
        for (Type genericParameterType : genericParameterTypes) {
            System.out.println("1.1 获取方法参数泛型:\n" + genericParameterType);
            // 1.1.1 获取真实参数类型
            if (genericParameterType instanceof ParameterizedType) {
                Type[] actualTypeArguments = ((ParameterizedType) genericParameterType).getActualTypeArguments();
                for (Type actualTypeArgument : actualTypeArguments) {
                    System.out.println("1.1.1 获取真实参数类型:\n" + actualTypeArgument);
                }
            }
        }

        System.out.println("\n");
        Method method2 = c1.getMethod("test02");
        // 1.2 获取方法返回值泛型
        Type genericReturnType = method2.getGenericReturnType();
        System.out.println("1.2 获取方法返回值泛型:\n" + genericReturnType);
        if (genericReturnType instanceof ParameterizedType) {
            // 1.2.1 获取真实参数类型
            Type[] actualTypeArguments = ((ParameterizedType) genericReturnType).getActualTypeArguments();
            for (Type actualTypeArgument : actualTypeArguments) {
                System.out.println("1.2.1 获取真实参数类型:\n" + actualTypeArgument);
            }
        }
    }

    public void test01(Map<String, User> map, List<User> list) throws Exception{
        System.out.println("test01");
    }

    public Map<String, User> test02() {
        System.out.println("test02");
        return null;
    }
}
